Ext.BLANK_IMAGE_URL='/wsd/image/page/s.gif';
Ext.onReady(function(){
	var parent_id = 0;//默认类别为父类别
	var parent_level = 0;
	var c_node = null;
	var categoryTree = new Ext.tree.TreePanel({
		title:'业务',
		region:'west',
		frame:true,
		width:200,
		split:true,
		collapsible:true,
		rootVisible : false,
		autoScroll : true,
		
		root:new Ext.tree.AsyncTreeNode({
			id : '0',
			text : '类别'
		}),
		loader: new Ext.tree.TreeLoader({
			url:'/wsd/admin/category_categoryTreeData.action'
    	})
	});
	categoryTree.expandAll();
		
	
	var categoryField = new Ext.form.TextField({
		fieldLabel:'名称',
		value:'默认类别',
		readOnly:true,
		width:200
	})
	
	var delBtn = new Ext.Button({
		text:'删除类别',
		disabled:true,
		handler:function(){
			Ext.Msg.confirm("系统信息","确定要删除 " + c_node.attributes.text,function(btn){
				if(btn == 'yes'){
					delCategory(c_node);
				}
				
			});
			
		}
	})
	var modifyBtn = new Ext.Button({
		text:'修改类别名称',
		disabled:true,
		handler:function(){
			Ext.Msg.prompt("类别名称修改","请输入类别的新名称",function(btn,text){
				if(btn == 'ok'){
					modify(text);
				}
				
			});
			
		}
	})
	var form = new Ext.form.FormPanel({
		region:'center',
		frame:'true',
		items:[categoryField,
			{
				layout:'column',
				items:[
				delBtn,
				modifyBtn
				]
				
			}
		]
	})
	var viewport = new Ext.Viewport({
		layout : 'border',
		frame:true,
		items : [categoryTree,form]
	});
	categoryTree.on("click",function(node,e){
		parent_id = node.attributes.id;
		parent_level = node.attributes.level;
		if(parent_id != 0){
			c_node = node;
			delBtn.setDisabled(false);
			modifyBtn.setDisabled(false);
		}else{
			c_node = null;
			delBtn.setDisabled(true);
			modifyBtn.setDisabled(true);
		}
		if(parent_id == 1){
			delBtn.setDisabled(true);
		}
		categoryField.setValue(node.attributes.text);
	})
	
	/**
	 * 删除类别
	 */
	function delCategory(c_node){
		Ext.Msg.wait("正在删除请等待……");
		Ext.Ajax.request({
			url:'/wsd/admin/category_delCategorie.action',
			params:{
				'category.CId':c_node.attributes.id
			},
			success:function(response){
				var result = Ext.util.JSON.decode(response.responseText).result;
				var msg = result.msg;
				
				//var node = new Ext.tree.TreeNode();
				c_node.remove(true);
				Ext.Msg.hide();
				delBtn.setDisabled(true);
				modifyBtn.setDisabled(true);
				Ext.Msg.alert("系统信息",msg);
			},
			failure:function(response){
				Ext.Msg.alert('系统消息','删除失败');
			}
		});
	}
	
	function modify(text){
		Ext.Msg.wait("正在修改……");
		Ext.Ajax.request({
			url:'/wsd/admin/category_modifyCategorie.action',
			params:{
				'category.CId':c_node.attributes.id,
				'category.CName':text
			},
			success:function(response){
				var result = Ext.util.JSON.decode(response.responseText).result;
				var msg = result.msg;
				
				//var node = new Ext.tree.TreeNode();
				c_node.setText(text);
				categoryField.setValue(text);
				Ext.Msg.hide();
			},
			failure:function(response){
				Ext.Msg.alert('系统消息','删除失败');
			}
		});
		
	}
	
});